package com.zhny.aiot.water.level.ten.plugin;

import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.model.IRequestMessage;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.aiot.water.level.ten.plugin.model.WaterStoreEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WaterLevelDevice extends ModbusNumDevice<WaterStoreEntry> {
    private static final Logger logger = LoggerFactory.getLogger(WaterLevelDevice.class);
    public WaterLevelDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super(engine, factory, centerFacilities, entity);
    }

    @Override
    public WaterStoreEntry decode(ModemResponseMessage modemResponseMessage) {
//        WaterLessConfig config = (WaterLessConfig)getConfig();
        logger.info("start water less decode");
        WaterStoreEntry entry = new WaterStoreEntry();
        int waterless = modemResponseMessage.getData().readUnsignedShort();
//        int depth = StringUtils.isNullOrEmpty(config.getDepth())?0:Integer.parseInt(config.getDepth());
        entry.setWaterless(10*waterless/2000.0);
        logger.info("water less end decode {}",entry.toString());
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData().readableBytes()== 2;
    }

    @Override
    protected IRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory().getRequestMessage(getConfig());
    }

    @Override
    protected void disposable() {

    }
}
